package server

import (
	"bufio"
	"gitee.com/yuya000/cards-game-server-go/utils"
	"net"
)

type Session struct {
	Id     string
	conn   net.Conn
	reader *bufio.Reader
	writer *bufio.Writer
}

func NewSession(conn net.Conn) *Session {
	return &Session{
		Id:     utils.GenerateSessionId(),
		conn:   conn,
		reader: bufio.NewReader(conn),
		writer: bufio.NewWriter(conn),
	}
}

func (session *Session) Read(p []byte) (int, error) {
	return session.reader.Read(p)
}

func (session *Session) Write(p []byte) (int, error) {
	return session.writer.Write(p)
}

func (session *Session) Flush() error {
	return session.writer.Flush()
}

func (session *Session) Close() {
	session.writer.Flush()
	session.conn.Close()
}
